package com.android.client.presenter;

import java.io.Serializable;

import com.android.client.GreetingServiceAsync;
import com.android.client.entity.ConfEntity;
import com.android.client.event.ConfEditorStateSwitchEvent;
import com.android.client.handler.ConfEditorStateSwitchHanlder;
import com.android.client.view.panel.ConfEditPanel;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class StateSwitchPsenter extends AbstractPresenter {
	public static final String TYPE = "State_Switch";
	
	public StateSwitchPsenter(Widget view, HandlerManager eventBus,
			GreetingServiceAsync service) {
		super(view, eventBus, service);
	}

	@Override
	protected void bind() {
		pm.AddPresenter(TYPE, this);
		Button tempBatton = eventBusSetting();
		onCilck(tempBatton);
		onSelectChange();
	}
	
	private void onSelectChange(){
		final ConfEditPanel panel = (ConfEditPanel) view;
		final ListBox select = panel.getSelector();
		select.addChangeHandler(new ChangeHandler(){
			@Override
			public void onChange(ChangeEvent event) {
				Long groupid = new Long(select.getValue(select.getSelectedIndex()));
				ConfEditorStateSwitchEvent mEvent = new ConfEditorStateSwitchEvent();
				mEvent.setType(ConfEditorStateSwitchEvent.LOAD_CONF);
				mEvent.setConfJdo(new ConfEntity(groupid, null));
				eventBus.fireEvent(mEvent);
			}
			
		});
	}
	
	private void onCilck(final Button tempBatton) {
		tempBatton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				ConfEditorStateSwitchEvent mEvent = new ConfEditorStateSwitchEvent();
				mEvent.setType(ConfEditorStateSwitchEvent.SAVE_CONF);
				String text = tempBatton.getText();
				int state = 0;
				if(text.equals(ConfEditPanel.SUBMIT_TEXT)){
					state = 1;
				}
				mEvent.setState(state);
				eventBus.fireEvent(mEvent);
			}
		});
	}
	
	private Button eventBusSetting() {
		final ConfEditPanel panel = (ConfEditPanel) view;
		Button editorBatton = panel.getControlButton();
		eventBus.addHandler(ConfEditorStateSwitchEvent.TYPE, new ConfEditorStateSwitchHanlder() {
			public void ConfEditorStateSwitch(ConfEditorStateSwitchEvent event) {
				if(event.getType() != ConfEditorStateSwitchEvent.SAVE_CONF) return;
				switch(event.getState()){
				case ConfEditorStateSwitchEvent.EDITOR:
					panel.getConfEdit().setReadOnly(false);
					panel.getControlButton().setText(ConfEditPanel.SUBMIT_TEXT);
					break;
				case ConfEditorStateSwitchEvent.SUBMIT:
					panel.getConfEdit().setReadOnly(true);
					ListBox selector = panel.getSelector();
					String groupId = selector.getItemText(selector.getSelectedIndex());
					ConfEntity entity = new ConfEntity(new Long(groupId), panel.getConfEdit().getText());
					event.setConfJdo(entity);
					service.execute(event, new AsyncCallback<Serializable>(){
						public void onFailure(Throwable caught) {
						}
						
						public void onSuccess(Serializable result) {
							Boolean isOK = (Boolean) result;
							if(isOK){
								Window.alert("ok");
							}else{
								Window.alert("failed");
							}
						}
						
					});
					panel.getControlButton().setText(ConfEditPanel.EDITOR_TEXT);
					break;
				};
			}

			@Override
			public void confTextLoad(ConfEditorStateSwitchEvent event) {
				if(event.getType() != ConfEditorStateSwitchEvent.LOAD_CONF) return;
				service.execute(event, new AsyncCallback<Serializable>(){
					public void onFailure(Throwable caught) {
					}
					
					public void onSuccess(Serializable result) {
						panel.getConfEdit().setText((String) result);
					}
					
				});
				
			}
		});
		return editorBatton;
	}

}
